import Taro, { Component } from '@tarojs/taro'
import { View } from '@tarojs/components'
import { AtButton, AtForm, AtInput, AtSwitch } from 'taro-ui'

const http = require('../../utils/http.js')
const show = require('../../utils/show.js')
const constant = require('../../utils/constant.js')

export default class Login extends Component {
    config = {
        navigationBarTitleText: '极简笔记-登录'
    }

    constructor() {
        super(...arguments)
        this.state = {
            formObj: { remember: true }
        }
    }

    handleChangeAccount = (value) => this.handleChange('account', value)
    handleChangePassword = (value) => this.handleChange('password', value)
    handleChangeRemember = (value) => this.handleChange('remember', value)
    handleChange = (key, value) => {
        console.info(key, value)
        var formObj = this.state.formObj || {}
        formObj[key] = value
        this.setState({ formObj: formObj })
        return value
    }
    onSubmit = () => {
        if (!this.state.formObj.account) {
            show.warn('请输入账号')
            return
        }
        if (!this.state.formObj.password) {
            show.warn('请输入密码')
            return
        }
        http.post2('/api/security/login', this.state.formObj)
            .then(res => {
                if (res.login == 'ok') {
                    Taro.setStorageSync(constant.USER_TOKEN_KEY, res.token)
                    Taro.redirectTo({
                        url: constant.PAGE_INDEX
                    })
                } else {
                    show.warn('用户名或密码不对')
                }
            })
    }
    onPlatformLogin = () => {
        show.confirm({
            title: '是否授权登录',
            content: '本服务需要登录才能复习笔记。\n授权只会记录阁下 OpenID，不会记录其他任何账号信息',
            onOk: () => {
                Taro.login({
                    success: (res) => {
                        if (res.code) {
                            http.post2(
                                '/api/security/platformLogin',
                                { code: res.code, platform: process.env.TARO_ENV }
                            ).then(res => {
                                if (res.sign == 'ok') {
                                    Taro.setStorageSync(constant.USER_TOKEN_KEY, res.token)
                                    Taro.setStorageSync(constant.OPEN_ID_KEY, res.openId)
                                    Taro.redirectTo({
                                        url: constant.PAGE_INDEX
                                    })
                                } else if (res.sign == 'general') {
                                    show.warn('请先绑定账号')
                                    Taro.setStorageSync(constant.OPEN_ID_KEY, res.openId)
                                    Taro.redirectTo({
                                        url: '/pages/login/bind'
                                    })
                                } else {
                                    show.warn('登录出错！')
                                    console.log('登录出错！' + res.message)
                                }
                            })
                        } else {
                            show.warn('登录失败！')
                            console.log('登录失败！' + res.errMsg)
                        }
                    }
                })
            }
        })
    }

    render() {
        return (
            <View>
                <AtForm onSubmit={this.onSubmit}>
                    <AtInput
                        name='account'
                        title='账号'
                        type='text'
                        placeholder='请输入账号'
                        onChange={this.handleChangeAccount}
                        required={true}
                        clear={true}
                    />
                    <AtInput
                        name='password'
                        title='密码'
                        type='password'
                        placeholder='请输入密码'
                        onChange={this.handleChangePassword}
                        required={true}
                        clear={true}
                    />
                    <AtSwitch
                        title='记住我'
                        checked={this.state.formObj.remember}
                        onChange={this.handleChangeRemember}
                    />
                    <AtButton formType='submit' type='primary'>登录</AtButton>
                </AtForm>
                <AtButton
                    type='primary'
                    customStyle={{ color: '#8B0000', marginTop: '10rpx' }}
                    onClick={this.onPlatformLogin}
                >平台授权登录</AtButton>
            </View>
        )
    }
}